import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import ca.scotthyndman.web.swf.BuildInfo;
import ca.scotthyndman.web.swf.CompileException;
import ca.scotthyndman.web.swf.SWFCompilerBridge;

/**
 * Shows how to compile an ActionScript file using Java.
 * 
 * @author Scott Hyndman
 */
public class SwfmillCompileMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//
		// Create an object that describes what you want to build
		//
		BuildInfo info = new BuildInfo("Tuto.as", // file to compile
				"examples/swfmill_compile/library_xml/assets.xml", // swfmill library
				"examples/swfmill_compile/as"); // class path

		File swf = new File("examples/swfmill_compile/output.swf");
		try {
			//
			// Create a bridge and give it a temporary directory to compile in
			//
			SWFCompilerBridge bridge = new SWFCompilerBridge("examples/tmp",
					true);

			//
			// Compile!
			//
			File tmp = bridge.compileSWF(info);
			
			//
			// Now let's copy the file so you can take a look at it
			// (because the temporary file gets removed automatically)
			//
			copyFile(tmp, swf);
			
			//
			// Tell the bridge to clean up
			//
			bridge.cleanup();
		} catch (IOException e) {
			e.printStackTrace();
			return;
		} catch (CompileException e) {
			e.printStackTrace();
			return;
		}
		
		//
		// Now take a look in the /examples/simple_compile/ directory. You just
		// generated output.swf!
		//
	}

	/**
	 * A method to copy files. Nothing to do with the project.
	 * 
	 * @param in
	 * @param out
	 * @throws IOException 
	 */
	private static void copyFile(File in, File out) throws IOException {
		FileInputStream fis = new FileInputStream(in);
		FileOutputStream fos = new FileOutputStream(out);
		byte[] buf = new byte[1024];
		int i = 0;
		while ((i = fis.read(buf)) != -1) {
			fos.write(buf, 0, i);
		}
		fis.close();
		fos.close();
	}
}
